/**
 * ajax请求
 * @param {Object} Vue Vue的实例
 * @param {Object} options 请求配置公用
 * @return {Function} 返回当前@ajax
 */
 import axios from 'axios'  

 import {newWorkTextConfig, newWorkProConfig, SUCCESS_REQUEST_STATUS} from './config.js'
 export default {
	install(Vue, options){
		 // 默认参数
		 const DEFAULT_CONFIG = {
			 // 接口名称
			 api: '',
			 // 默认POST请求
			 type: 'POST',
			 // 请求参数
			 params: {},
		 }
		 Vue.prototype.$ajax = ((option) => {
			 // 拼接参数
			 if(!option) console.log('请求参数不能为空') 
			 let resData = Object.assign({},DEFAULT_CONFIG, option)
			 // 判断当前的请求方式如果是get合并请求
			let url =  resData.type == 'GET' ? `${newWorkTextConfig.b_server_url}${option.api}/${mergeUrl(resData)}` : `${newWorkTextConfig.b_server_url}${option.api}`
			 // 判断当前是否是普通的Content-Type
			 let contentType = resData.contentType ? 'application/json' : resData.params instanceof FormData  ? 'multipart/form-data' : 'application/x-www-form-urlencoded'
			 return new Promise((resolve, reject) => {
						axios({
								url: url,
								data: resData.params, 
								method: resData.type,
								transformRequest: [(data) => {
									// 如果当前是get请求
										let ret = ''
										if (resData.type == 'GET') {
											for (let it in resData.params) {
												ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
											}
										} else {
											// 处理请求参数
											ret = resData.params instanceof FormData ? resData.params : resData.contentType ? JSON.stringify(resData.params) : handleParams(resData.params)
										}
										return ret
							}],
							headers: {
                    'Content-Type': contentType
              }
						}).then(res => {
							
								if(res.data.status === SUCCESS_REQUEST_STATUS) {
									resolve({
										data: res.data.data,
										status: res.data.status
									})
									return
								} else{
									
									reject({
										status: res.data.status,
										message: res.data.message
									})
								}
						}).catch(err => {
								reject({
									message: '服务器报错'
								})
						})
						
			 })
		 })
	 }
 }
 
	/**
	* @author yang 
	*	@param param 请求的参数
	* @return 返回拼接后的参url参数
	*/
 const mergeUrl = (options) => {
		let url = ''
		for(let k in options.params) {
			let value = options.params[k] !== undefined ? options.params[k] : ''
			url += '&' + k + '=' + encodeURIComponent(value)
		}
		return url ? url.substring(1) : ''
 }
 
 /**
	* @author yang 
	* @param param 请求的参数
	* @return 返回拼接后的参数
	*/
 const handleParams = (param) => {
	  let params = []
	  let result = null
	  let keys = Object.keys(param)
		for (let i = 0, len = keys.length; i < len; i++) {
			// 去除空参数
			if (param[keys[i]] !== undefined  && param[keys[i]] !== null ) {
				param[keys[i]] = encodeURIComponent(param[keys[i]])
				params.push(keys[i] + '=' + param[keys[i]])
			}
		}
		result = params.join('&')
	 return result
 }
 
 
 const handleJsonParams = (param) =>  {
	 alert(1)
	 console.log(param)
	 console.log('777')
	 let keys = Object.keys(param)
	for (let i = 0, len = keys.length; i < len; i++) {
		// 去除空参数
		if (param[keys[i]] !== undefined  && param[keys[i]] !== null ) {
				param[keys[i]] = param[keys[i]]
				console.log(param[keys[i]])
		}
	}
 }
 